﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FILEtoJPG
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 是否安装了Winrar
        /// </summary>
        /// <returns></returns>
        static public bool Exists()
        {
            RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());
        }
        string path;
        string repath;
        private void btn_file_Click(object sender, EventArgs e)
        {
            //删除临时rar文件
           
            if (File.Exists(path + "\\myFILEtoJPGtemp.rar"))
            {
                File.Delete(path + "\\myFILEtoJPGtemp.rar");
            }
            OpenFileDialog openFileDialog_file = new OpenFileDialog();
            openFileDialog_file.Multiselect = true;
            openFileDialog_file.Title = "请选择需要隐藏的文件";
            openFileDialog_file.Filter = "所有文件(*.*)|*.*";
            if (openFileDialog_file.ShowDialog() == DialogResult.OK)
            {
                string files="";                
                foreach (string s in openFileDialog_file.FileNames)
                {                    
                    string patch = System.IO.Path.GetFileName(s);                    
                    string rarPatch = System.IO.Path.GetDirectoryName(s);
                    path = rarPatch;
                    string rarName = "myFILEtoJPGtemp.rar";
                    CompressRAR(patch, rarPatch, rarName);
                    files += patch + " ";
                    tb_file.Text = files;
                }
                
                //MessageBox.Show("已选择文件:" + file, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_pic_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog_pic = new OpenFileDialog();
            //openFileDialog_pic.Multiselect = true;
            openFileDialog_pic.Title = "请选择用于显示的图片文件";
            openFileDialog_pic.Filter = "图片文件(*.jpg)|*.jpg";
            if (openFileDialog_pic.ShowDialog() == DialogResult.OK)
            {
                string pic = openFileDialog_pic.FileName;
                tb_pic.Text = pic;
                //MessageBox.Show("已选择文件:" + file, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                pictureBox1.Image = Image.FromFile(pic);
            }

        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string file = path +"\\myFILEtoJPGtemp.rar";
            string pic = tb_pic.Text;
            string save;

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "合成和保存为";
            saveFileDialog.Filter = "图片文件(*.jpg)|*.jpg";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                save = saveFileDialog.FileName;


                //调用合成函数
                compose2(file, pic, save);

                //string strVersion = myProcess.StandardOutput.ReadToEnd();//从输出流获取命令执行结果
                //tb_save.Text = save;
                MessageBox.Show("已保存到:" + save, "保存文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //删除临时rar文件
            //System.Threading.Thread.Sleep(5);
            if (File.Exists(path + "\\myFILEtoJPGtemp.rar"))
            {
                File.Delete(path + "\\myFILEtoJPGtemp.rar");
            }
        }

        //void compose(string file, string pic, string save)
        //{
        //    makebat(file, pic, save);
        //    runbat();

        //}
        //void makebat(string file, string pic, string save)
        //{

        //    FileStream fs = new FileStream("compose.bat", FileMode.Create);
        //    StreamWriter sw = new StreamWriter(fs, encoding: Encoding.GetEncoding("gb2312"));
        //    //开始写入
        //    sw.Write("copy /b" + " \"" + pic + "\" " + "+" + " \"" + file + "\" " + "\"" + save + "\" ");
        //    //清空缓冲区
        //    sw.Flush();
        //    //关闭流
        //    sw.Close();
        //    fs.Close();
        //}
        //void runbat()
        //{
        //    Process proc = null;
        //    try
        //    {
        //        proc = new Process();
        //        proc.StartInfo.FileName = @"compose.bat";
        //        proc.StartInfo.Arguments = string.Format("10");//this is argument
        //        proc.StartInfo.CreateNoWindow = false;
        //        proc.Start();
        //        proc.WaitForExit();
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
        //    }
        //}

        void compose2(string file, string pic, string save)
        {
           //合成
            Process myProcess = new Process();
            myProcess.StartInfo.FileName = "cmd.exe";//设定程序名

            myProcess.StartInfo.UseShellExecute = false;//关闭Shell的使用

            myProcess.StartInfo.RedirectStandardInput = true;//重定向标准输入

            myProcess.StartInfo.RedirectStandardOutput = true;//重定向标准输出

            myProcess.StartInfo.RedirectStandardError = true;//重定向标准错误输出

            myProcess.StartInfo.CreateNoWindow = true;//设置不显示窗口

            myProcess.Start();
            myProcess.StandardInput.WriteLine("copy /b" + " \"" + pic + "\" " + "+" + " \"" + file + "\" " + "\"" + save + "\" ");
            myProcess.StandardInput.WriteLine("exit");


        }

        private void btn_reFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog_pic = new OpenFileDialog();
            //openFileDialog_pic.Multiselect = true;
            openFileDialog_pic.Title = "请选择需要被释放的文件";
            openFileDialog_pic.Filter = "图片文件(*.jpg)|*.jpg";
            if (openFileDialog_pic.ShowDialog() == DialogResult.OK)
            {
                string reFile = openFileDialog_pic.FileName;
                tb_reFile.Text = reFile;
                repath = System.IO.Path.GetDirectoryName(reFile);
                pictureBox1.Image = Image.FromFile(reFile);
            }
        }

        private void btn_release_Click(object sender, EventArgs e)
        {
            string reFile = tb_reFile.Text;
            string reSave;
           

            FolderBrowserDialog saveFileDialog = new FolderBrowserDialog();
            saveFileDialog.Description  = "释放文件到…";
            saveFileDialog.SelectedPath = repath;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                reSave = saveFileDialog.SelectedPath; ;
              
                //调用合成函数
               release(reFile, reSave);
               MessageBox.Show("已释放到:" + reSave, "保存文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        int release(string reFile, string reSave)
        {
            //释放
            Process myProcess = new Process();
            myProcess.StartInfo.FileName = "cmd.exe";//设定程序名

            myProcess.StartInfo.UseShellExecute = false;//关闭Shell的使用

            myProcess.StartInfo.RedirectStandardInput = true;//重定向标准输入

            myProcess.StartInfo.RedirectStandardOutput = true;//重定向标准输出

            myProcess.StartInfo.RedirectStandardError = true;//重定向标准错误输出

            myProcess.StartInfo.CreateNoWindow = true;//设置不显示窗口

            myProcess.Start();
            myProcess.StandardInput.WriteLine("copy /b" + " \"" + reFile + "\" " + "\"" + reSave + "\\myFILEtoJPGtemp.rar" + "\" ");
            myProcess.StandardInput.WriteLine("exit");

            //解压
            string unRarPatch = reSave;
            string rarPatch = reSave;
            string rarName = "myFILEtoJPGtemp.rar";
            unCompressRAR(unRarPatch, rarPatch, rarName);

            //删除临时rar文件
            if (File.Exists(reSave + "\\myFILEtoJPGtemp.rar"))
            {
                File.Delete(reSave + "\\myFILEtoJPGtemp.rar");
            }
            return 0;
        }
           
        public void unCompressRAR(string unRarPatch, string rarPatch, string rarName)
        {
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;


            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                //the_rar = the_rar.Substring(1, the_rar.Length - 7);

                if (Directory.Exists(unRarPatch) == false)
                {
                    Directory.CreateDirectory(unRarPatch);
                }
                the_Info = "x " + rarName + " " + unRarPatch + " -y";

                ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径

                Process the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
        }
        public void CompressRAR(string patch, string rarPatch, string rarName)
        {
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                //the_rar = the_rar.Substring(1, the_rar.Length - 7);建议删除
                //Directory.CreateDirectory(patch);
                //命令参数
                //the_Info = " a    " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
                the_Info = " a    " + rarName + " " + patch  ;
                the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //打包文件存放目录
                the_StartInfo.WorkingDirectory = rarPatch;
                the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
       
        private void btn_about_Click(object sender, EventArgs e)
        {
            Form aboutbox = new AboutBox();
            aboutbox.Show ();
        }



        private void btn_help_Click(object sender, EventArgs e)
        {
            //Form Help = new Form();
            //Help.Show();
            if (richTBHelp.Visible == false)
            {
                btn_help.Text = "关闭帮助";
                groupBox3.Text = "帮助";
                richTBHelp.Visible = true;
            }
            else
            {
                btn_help.Text = "查看帮助";
                groupBox3.Text = "图像预览";
                richTBHelp.Visible = false;
            }

        }
        private void richTBHelp_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText); 
        }
    }
   
}
      